package com.pingtai.ajax.action;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.util.Date;
import java.util.Iterator;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.htmlparser.util.ParserException;

import com.pingtai.ajax.entity.IndentTarget_163;
import com.pingtai.ajax.entity.Notice;
import com.pingtai.ajax.service.NoticeService;
import com.pingtai.indent.entity.RenzhengMid;
import com.pingtai.indent.entity.TencentBusinessRenzheng;
import com.pingtai.indent.entity.TencentPersonRenzheng;
import com.pingtai.indent.entity.UserBaseIndentType;
import com.pingtai.indent.entity.UserIndent;
import com.pingtai.indent.entity.UserIndentType;
import com.pingtai.indent.entity.XinlangPersonRenzheng;
import com.pingtai.indent.service.WeiboIndentService;
import com.pingtai.user.entity.User;
import com.pingtai.user.entity.UserPay;
import com.pingtai.user.service.UserManagerService;
import com.pingtai.util.HttpClientUtils;
import com.pingtai.util.Utils;

public class NoticeAction {

	public Notice notice;

	public NoticeService noticeService;

	private UserManagerService userManagerService;

	private WeiboIndentService weiboIndentService;

	private String userId;

	private int result;

	private String base;

	private String targetUrl;

	private IndentTarget_163 indentTarget_163;

	private Integer indentId;

	private String indentIds;

	private String state;

	private Integer startNumber;
	
	//用于获取城市列表、
	private String a;
	private String pid;
	private String cityMsg;
	
	

	// public NoticeService getNoticeService() {
	// return noticeService;
	// }

	public String getCityMsg() {
		return cityMsg;
	}

	public void setCityMsg(String cityMsg) {
		this.cityMsg = cityMsg;
	}

	public String getA() {
		return a;
	}

	public void setA(String a) {
		this.a = a;
	}

	public String getPid() {
		return pid;
	}

	public void setPid(String pid) {
		this.pid = pid;
	}

	public IndentTarget_163 getIndentTarget_163() {
		return indentTarget_163;
	}

	public Integer getStartNumber() {
		return startNumber;
	}

	public void setStartNumber(Integer startNumber) {
		this.startNumber = startNumber;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getIndentIds() {
		return indentIds;
	}

	public void setIndentIds(String indentIds) {
		this.indentIds = indentIds;
	}

	public WeiboIndentService getWeiboIndentService() {
		return weiboIndentService;
	}

	public void setWeiboIndentService(WeiboIndentService weiboIndentService) {
		this.weiboIndentService = weiboIndentService;
	}

	public Integer getIndentId() {
		return indentId;
	}

	public void setIndentId(Integer indentId) {
		this.indentId = indentId;
	}

	public void setIndentTarget_163(IndentTarget_163 indentTarget_163) {
		this.indentTarget_163 = indentTarget_163;
	}

	public String getBase() {
		return base;
	}

	public void setBase(String base) {
		this.base = base;
	}

	public String getTargetUrl() {
		return targetUrl;
	}

	public void setTargetUrl(String targetUrl) {
		this.targetUrl = targetUrl;
	}

	public int getResult() {
		return result;
	}

	public void setResult(int result) {
		this.result = result;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public UserManagerService getUserManagerService() {
		return userManagerService;
	}

	public void setUserManagerService(UserManagerService userManagerService) {
		this.userManagerService = userManagerService;
	}

	public void setNoticeService(NoticeService noticeService) {
		this.noticeService = noticeService;
	}

	public void setNotice(Notice notice) {
		this.notice = notice;
	}

	public Notice getNotice() {
		return this.notice;
	}

	public String getLastNotice() {
		this.notice = noticeService.getLastNotice();
		notice.setCreateDate(null);
		notice.setCreateUser(null);
		notice.setChangeDate(null);
		notice.setChangeUser(null);
		return "success";
	}

	public String changeToDefaultPass() {
		userManagerService.updatePassword(new Integer(userId),
				Integer.toString(123456), false);
		result = 1;
		return "success";
	}

	public String dongjieUser() {
		userManagerService.updateEnabled(new Integer(userId), 2);
		result = 1;
		return "success";
	}

	public String jiedongUser() {
		userManagerService.updateEnabled(new Integer(userId), 1);
		result = 1;
		return "success";
	}

	public String getIndentTargetInfo_xinlang() throws MalformedURLException,
			ParserException, IOException {
		indentTarget_163 = noticeService.getIndentTargetInfo_xinlang(targetUrl);
		return "success";
	}

	public String getIndentTargetInfo_163() throws MalformedURLException,
			ParserException, IOException {
		indentTarget_163 = noticeService.getIndentTarget_163(targetUrl);
		return "success";
	}

	public String auditWeiboIndentFor163() {
		result = 0;
		UserIndent userIndent = weiboIndentService.getUserIndentById(indentId);
		userIndent.setState(1);
		userIndent.setAuditDate(new Date());
		userIndent.setAuditUser(userManagerService.findCurrectUser());

		User cUser = userManagerService.findCurrectUser();
		int indentBaseTypeId = userIndent.getBaseIndentType().getId();
		Iterator it = cUser.getUserIndentType().iterator();
		boolean flag = false;

		while (it.hasNext()) {
			UserIndentType type = (UserIndentType) it.next();
			int id = type.getUserBaseIndentType().getId();
			if (indentBaseTypeId == id) {
				flag = true;
				break;
			}
		}

		if (userManagerService.isRoot(cUser)) {
			flag = true;
		}

		if (flag) {
			weiboIndentService.updateUserIndent(userIndent);
			result = 1;
		} else {
			result = 0;
		}

		return "success";
	}

	public String auditWeiboIndentForqq_zbpl() {
		result = 0;
		UserIndent userIndent = weiboIndentService.getUserIndentById(indentId);
		userIndent.setState(1);
		userIndent.setAuditDate(new Date());
		userIndent.setAuditUser(userManagerService.findCurrectUser());

		User cUser = userManagerService.findCurrectUser();
		int indentBaseTypeId = userIndent.getBaseIndentType().getId();
		Iterator it = cUser.getUserIndentType().iterator();
		boolean flag = false;

		while (it.hasNext()) {
			UserIndentType type = (UserIndentType) it.next();
			int id = type.getUserBaseIndentType().getId();
			if (indentBaseTypeId == id) {
				flag = true;
				break;
			}
		}

		if (userManagerService.isRoot(cUser)) {
			flag = true;
		}

		if (flag) {
			weiboIndentService.updateUserIndent(userIndent);
			result = 1;
		} else {
			result = 0;
		}

		return "success";
	}

	public String getIndentTargetInfo_qq() throws MalformedURLException,
			ParserException, IOException {
		indentTarget_163 = noticeService.getIndentTargetInfo_qq(targetUrl);
		return "success";
	}
	
	public String getIndentTargetInfo_taobao_kt() throws Exception{
		indentTarget_163 = noticeService.getIndentTargetInfo_taobao_kt(targetUrl);
		return "success";
	}
	
	public String getIndentTargetInfo_taobao_uz() throws Exception{
		indentTarget_163 = noticeService.getIndentTargetInfo_taobao_uz(targetUrl);
		return "success";
	}

	public String getIndentTargetInfo_qq_zbpl() throws ParserException,
			IOException {
		indentTarget_163 = noticeService.getIndentTargetInfo_qq_zbpl(targetUrl);
		return "success";
	}

	public String getXlBlogGuanzhuTarget() throws Exception{
		indentTarget_163 = noticeService.getIndentTargetInfo_xl_blog_gz(targetUrl);
		return "success";
	}
	public String getXlBlogFangwenTarget() throws Exception{
		indentTarget_163 = noticeService.getIndentTargetInfo_xl_blog_fw(targetUrl);
		return "success";
	}
	
	public String getIndentTargetInfo_sohu() throws ParserException,
			IOException {
		indentTarget_163 = noticeService.getIndentTargetInfo_sohu(targetUrl);
		return "success";
	}

	
	
	public String getIndentTargetInfo_xinlang_nologin() throws ParserException,
			IOException {
		indentTarget_163 = noticeService
				.getIndentTargetInfo_xinlang_nologin(targetUrl);
		return "success";
	}

	public String batchTuikuan() {

		result = 0;

		String[] ind_id = indentIds.split(",");

		for (int i = 0; i < ind_id.length; i++) {
			UserIndent userIndent1 = weiboIndentService
					.getUserIndentById(new Integer(ind_id[i]));
			User curUser = userManagerService.findCurrectUser();
			userIndent1.setState(new Integer(state));
			userIndent1.setRefundDate(new Date());
			userIndent1.setRefundUser(curUser);
			weiboIndentService.saveUserIndent(userIndent1);
			UserPay userPay = new UserPay();

			User childUser = userIndent1.getUser();
			userPay.setChildUser(childUser);
			userPay.setUser(curUser);
			userPay.setCreateDate(new Date());
			userPay.setMoney(userIndent1.getTotalPrice());
			userPay.setType(1);
			userPay.setUserAllMoney(childUser.getMoney()
					+ userIndent1.getTotalPrice());
			userPay.setUserIndent(userIndent1);
			userPay.setType2(4);
			userPay.setUserBaseIndent(userIndent1.getBaseIndentType());
			userPay.setIndentCreateUser(userIndent1.getUser());
			userManagerService.saveUserPay(userPay);
			childUser.setMoney(childUser.getMoney()
					+ userIndent1.getTotalPrice());
			userManagerService.saveOrUpdateUser(childUser);
		}

		result = 1;
		return "success";
	}

	public String batchShenhe() {
		result = 0;

		String[] ind_id = indentIds.split(",");

		for (int i = 0; i < ind_id.length; i++) {
			indentId = new Integer(ind_id[i]);
			auditWeiboIndentFor163();
		}

		result = 1;
		return "success";
	}

	public String getIndentTargetInfo_taobao_weitao() throws MalformedURLException, ParserException, IOException {
		targetUrl = targetUrl.replace("detail.tmall", "item.taobao");

		
		indentTarget_163 = noticeService
					.getIndentTargetInfo_taobao_weitao(targetUrl);
		
		return "success";
	}
	
	public String getIndentTargetInfo_taobao_tm_gz() throws ParserException, IOException{
		indentTarget_163 = noticeService
				.getIndentTargetInfo_taobao_tm_gz(targetUrl);
		return "success";
	}

	public String getIndentTargetInfo_taobao() throws MalformedURLException,
			ParserException, IOException {

		targetUrl = targetUrl.replace("detail.tmall", "item.taobao");

		if (targetUrl.indexOf("item.taobao.com") != -1) {
			Integer b = new Integer(base);
			if (!b.equals(30))
				return "success";
			indentTarget_163 = noticeService
					.getIndentTargetInfo_taobao_baobei(targetUrl);
		} else {
			Integer b = new Integer(base);
			if (!b.equals(34))
				return "success";
			targetUrl = targetUrl
					.substring(0, targetUrl.indexOf(".taobao.com"))
					+ ".taobao.com/dongtai.htm";
			indentTarget_163 = noticeService
					.getIndentTargetInfo_taobao_dianpu(targetUrl);
		}

		return "success";
	}

	public String auditWeiboIndentForqq_zan() {
		result = 0;
		UserIndent userIndent = weiboIndentService.getUserIndentById(indentId);
		userIndent.setState(1);
		userIndent.setStartNumber(startNumber);
		userIndent.setNowNumber(startNumber);
		userIndent.setAuditDate(new Date());
		userIndent.setAuditUser(userManagerService.findCurrectUser());

		User cUser = userManagerService.findCurrectUser();
		int indentBaseTypeId = userIndent.getBaseIndentType().getId();
		Iterator it = cUser.getUserIndentType().iterator();
		boolean flag = false;

		while (it.hasNext()) {
			UserIndentType type = (UserIndentType) it.next();
			int id = type.getUserBaseIndentType().getId();
			if (indentBaseTypeId == id) {
				flag = true;
				break;
			}
		}

		if (userManagerService.isRoot(cUser)) {
			flag = true;
		}

		if (flag) {
			weiboIndentService.updateUserIndent(userIndent);
			result = 1;
		} else {
			result = 0;
		}

		return "success";
	}

	public String jiesuanWeiboIndentForRenzheng() {
		result = 0;
		boolean flag = false;
		UserIndent userIndent = weiboIndentService.getUserIndentById(indentId);
		RenzhengMid renzhengMid = userIndent.getRenzheng();
		TencentBusinessRenzheng tenB = renzhengMid.getTencentBusinessRenzheng();
		TencentPersonRenzheng tenP = renzhengMid.getTencentPersonRenzheng();
		XinlangPersonRenzheng xinP = renzhengMid.getXinlangPersonRenzheng();

		if (tenB != null) {
			tenB.setJiesuan(1);
		}

		if (tenP != null) {
			tenP.setJiesuan(1);
		}

		User cUser = userManagerService.findCurrectUser();

		if (userManagerService.isRoot(cUser)
				|| userManagerService.isAdmin(cUser)) {
			flag = true;
		}

		if (flag) {
			weiboIndentService.updateUserIndent(userIndent);
			result = 1;
		} else {
			result = 0;
		}

		return "success";
	}

	public String batchAddUserIndentTypeBasePrice() {
		result = 0;

		result = weiboIndentService.batchAddUserIndentTypeBasePrice();

		return "success";
	}

	public String batchAddBaseIndentTypeUpdateDate() {
		result = 0;

		result = weiboIndentService.batchAddBaseIndentTypeUpdateDate();

		return "success";
	}
	
	public String updateIndentPay(){
		weiboIndentService.updateIndentPay();
		return "success";
	}
	
	public String WeixinIndentComplete(){
		result=0;
		//int nowNumber = userIndent.getNowNumber();
		UserIndent userIndent = weiboIndentService.getUserIndentById(indentId);
		UserBaseIndentType baseIndentType = userIndent.getBaseIndentType();
		User completeUser = userManagerService.findCurrectUser();
		if(userManagerService.isAdmin(completeUser)||
				userManagerService.isAudit(completeUser)||
				userManagerService.isRoot(completeUser)){
			
		}else{
			return "success";
		}
		//调用返利方法
		try {
			userManagerService.userIndentRebate(userIndent);
		} catch (Exception e) {
			e.printStackTrace();
		}
		userIndent.setState(2);
		userIndent.setEndDate(new Date());
		userIndent.setEndUser(completeUser);
		weiboIndentService.updateUserIndentForCompleate(userIndent);
		result=1;
		return "success";
	}
	
//	public String city() throws Exception{
//		String url="http://www.actphp.com/ajaxcity/city.php?a="+a;
//		if(pid!=null) url = url + "&pid="+pid;
//		HttpClient client = HttpClientUtils.getHttpClient();
//		HttpGet httpGet = new HttpGet(url);
//		httpGet.setHeader("Content-Type", "application/x-www-form-urlencoded");  
//		httpGet.setHeader("Accept-Language","zh-CN,zh;q=0.8");  
//		httpGet.setHeader("Connection","Keep-Alive");  
//		httpGet.setHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1");  
//		httpGet.setHeader("Host","www.actphp.com"); 
//		httpGet.setHeader("Referer","http://www.actphp.com/ajaxcity/"); 
//		httpGet.setHeader("X-Requested-With","XMLHttpRequest");
//		httpGet.setHeader("Cookie", "saeut=218.28.80.163.1386051945082404; Hm_lvt_a44a348637bf1da22bc6332803948ecf=1386051885,1386230853; Hm_lpvt_a44a348637bf1da22bc6332803948ecf=1386232303; CNZZDATA3489763=cnzz_eid%3D1706452536-1386051945-http%253A%252F%252Fwww.actphp.com%26ntime%3D1386230911%26cnzz_a%3D2%26sin%3Dhttp%253A%252F%252Fwww.baidu.com%252Fs%253Ftn%253Dbaiduhome_pg%2526ie%253Dutf-8%2526bs%253D%2525E5%252588%2525B7%2525E7%2525B2%252589%2525E8%2525BF%25259D%2525E6%2525B3%252595%2525E5%252590%252597%2526f%253D8%2526rsv_bp%253D1%2526wd%253Dajaxcity%2526rsv_sug3%253D9%2526rsv_sug%253D0%2526rsv_sug1%253D8%2526rsv_sug4%253D400%2526inputT%253D3351%26ltime%3D1386230853184%26rtime%3D1");
//        HttpResponse response = client.execute(httpGet);
//        InputStream in = response.getEntity().getContent();
//        BufferedInputStream bin = new BufferedInputStream(in);
//        String msg = Utils.InputStreamTOString(in, "utf-8");
//		msg = Utils.unicodeToString(msg);
//		cityMsg = msg;
//		return "success";
//	}
	
	
	
}
